﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LGSVR.Common;
using LGSVR.BLL.Sys;
using System.Data;
using LGSVR.BLL.Program;


namespace LGSVR_WEB.sys
{
    public partial class UserActEdit : DataBasePage
    {
        private int Pid
        {
            get
            {
                return string.IsNullOrEmpty(Request.QueryString["ID"]) ? 0 : int.Parse(Request.QueryString["ID"]);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "out", "<script>alert('系统超时或服务器故障，请退出重新登录！');window.parent.parent.navigate('../Logout.aspx');</script>");
            }
            if (!IsPostBack)
            {
                PageInit();
            }
        }

       

        private void PageInit()
        {

            Dictionary<string, string> dicSingle;
            // 编辑
            if (!string.IsNullOrEmpty(Request.QueryString["action"]) && Request.QueryString["action"].ToLower() == "edit")
            {
                btnSave.Text = "更 新";
                if (Pid != 0)
                {
                    dicSingle = GetSigle("UserAct", "ID=" + Request.QueryString["ID"]);

                    if (dicSingle != null)
                    {                      
                        txtActID.Text = dicSingle["ActID"];
                        txtActName.Text = dicSingle["ActName"];
                        chkstate.Checked = Utils.ToBool(dicSingle["state"]);
                    }
                    else
                    {
                        MsgContent = ShowMessage("数据加载失败。", MessageType.Error, true, false);
                    }
                }
            }           
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtActID.Text.Trim()) || string.IsNullOrEmpty(txtActName.Text.Trim()))
            {
                MsgContent = ShowMessage("请填写角色编号和角色名称！", MessageType.Error, false, true);
                return;
            }
            else if (HasKey("userAct", "ActID", txtActID.Text.Trim(), Pid))
            {
                MsgContent = ShowMessage("已存在该角色编号，请更换！", MessageType.Error, false, true);
                return;
            }
            Dictionary<string, string> dicParam;

            dicParam = new Dictionary<string, string>();

            dicParam.Add("ActID", txtActID.Text.Trim());
            dicParam.Add("ActName", txtActName.Text.Trim());
            dicParam.Add("state", chkstate.Checked ? "1" : "0");

            // 编辑
            if (!string.IsNullOrEmpty(Request.QueryString["action"]) && Request.QueryString["action"].ToLower() == "edit")
            {
                if (Utils.IsNumber(Request.QueryString["ID"]))
                {
                    //得到原始actID
                    MessageInfo msgInfo = new MessageInfo();
                    string _actID = msgInfo.GetstrValue("SELECT actID  FROM userAct WHERE ID = " + Request.QueryString["ID"]);

                    Edit("userAct", dicParam, "ID=" + Request.QueryString["ID"]);

                    if (Result > 0)
                    {
                        //更新用户菜单权限表
                        Dictionary<string, string> dicParamActMenu;

                        dicParamActMenu = new Dictionary<string, string>();

                        dicParamActMenu.Add("ActID", txtActID.Text.Trim());
                        Edit("ActMenu", dicParamActMenu, "actID='" + _actID + "'");
                        if (Result > 0)
                        {
                            MsgContent = ShowMessage("恭喜您，更新成功！", MessageType.Success, false, true);
                            NoRefresh();
                            return;
                        }
                    }
                    //else
                    //{
                        if (Result == -100)
                        {
                            MsgContent = ShowMessage("对不起，操作失败，程序参数异常。", MessageType.Error, false, true);
                        }
                        else
                        {
                            MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
                        }
                    //}
                }
            }
            else  // 新增
            {
                try
                {
                    Insert("userAct", dicParam);

                    if (Result > 0)
                    {
                        //得到菜单信息                   
                        DataTable dtMenu;
                        int totalRS;
                        dtMenu = GetList("menuInfo", " id desc ", "", 1, Int32.MaxValue, out totalRS);
                        foreach (DataRow dr in dtMenu.Rows)
                        {
                            //往权限表里添加记录
                            Dictionary<string, string> dicParamuserAct;
                            dicParamuserAct = new Dictionary<string, string>();
                            dicParamuserAct.Add("menuID", dr["menuID"].ToString());
                            dicParamuserAct.Add("ActID", txtActID.Text.Trim());
                            string strstate = txtActName.Text.Trim().Contains("超级") ? "1" : "0";
                            dicParamuserAct.Add("state", strstate);
                            Insert("ActMenu", dicParamuserAct);
                        }


                        MsgContent = ShowMessage("恭喜您，添加成功！", MessageType.Success, false, true);
                        NoRefresh();
                    }
                }
                catch
                {
                    //else
                    //{
                    MsgContent = ShowMessage("对不起,操作失败，请重试。", MessageType.Error, false, true);
                    //}

                }
                
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["backUrlParam"]))
            {
                Response.Redirect("UserActList.aspx?" + Request.QueryString["backUrlParam"], false);
            }
            else
            {
                Response.Redirect("UserActList.aspx", false);
            }
        }
    }

}